package com.xiaoyu.lanling.feature.moment.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.FragmentActivity
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.base.view.OnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.moment.data.MomentData
import com.xiaoyu.lanling.feature.moment.dialog.MomentDeleteDialog
import com.xiaoyu.lanling.feature.moment.dialog.MomentReportDialog
import com.xiaoyu.lanling.feature.moment.model.MomentItem
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.util.TextViewUtil
import com.xiaoyu.lanling.view.UserAvatarDraweeView
import com.xiaoyu.lanling.view.text.UserNameTextView
import com.xiaoyu.lanling.widget.momentgrid.MomentGridLayoutView

class MyMomentViewHolder : ViewHolderBase<MomentItem>() {

    private lateinit var rootView: View
    private lateinit var avatar: UserAvatarDraweeView
    private lateinit var name: UserNameTextView
    private lateinit var address: TextView
    private lateinit var accost: ImageButton
    private lateinit var vipIcon: SimpleDraweeView
    private lateinit var verified: ImageView
    private lateinit var content: TextView
    private lateinit var images: MomentGridLayoutView
    private lateinit var age: TextView
    private lateinit var like: CheckBox
    private lateinit var comment: TextView
    private lateinit var action: ImageView

    private val onActionClickListener = object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            val itemData = v.getViewTag<MomentItem?>() ?: return
            val isOwner = itemData.feed.isOwner
            val activity = (App.getInstance().topActivity as? FragmentActivity) ?: return
            if (isOwner) {
                MomentDeleteDialog.newInstance(itemData.user.uid, itemData.feed.id).show(activity.supportFragmentManager, MomentDeleteDialog.TAG)
            } else {
                MomentReportDialog.newInstance(itemData.feed.id, "", ArrayList(itemData.feed.images.map { it.imageUrl })).show(activity.supportFragmentManager, MomentReportDialog.TAG)
            }
        }
    }

    // NOTE 这里可以公用一个 listener ，但是状态的改变会导致其他 button 也会触发这里的回调，所以 在 showData 里面，
    // 先置空，然后设置 listener
    private val onLikeCheckedListener = object : CompoundButton.OnCheckedChangeListener {
        override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
            val moment = buttonView.getViewTag<MomentItem>() ?: return
            MomentData.like(Any(), moment, isChecked)
        }
    }

    val onItemClickListener = object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            val activity = App.getInstance().topActivity ?: return
            val moment = v.getViewTag<MomentItem>() ?: return
            Router.instance.gotoMomentDetailActivity(activity, moment.feed.id)
        }
    }

    val onAvatarClickListener = object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            App.getInstance().topActivity?.finishAfterTransition()
        }
    }

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        rootView = layoutInflater.inflate(R.layout.moment_item, parent, false)
        avatar = rootView.findViewById(R.id.avatar)
        name = rootView.findViewById(R.id.name)
        address = rootView.findViewById(R.id.address)
        accost = rootView.findViewById(R.id.accost)
        verified = rootView.findViewById(R.id.verified)
        vipIcon = rootView.findViewById(R.id.vip)
        content = rootView.findViewById(R.id.feed_content)
        images = rootView.findViewById(R.id.moment_images)
        age = rootView.findViewById(R.id.age)
        like = rootView.findViewById(R.id.like)
        comment = rootView.findViewById(R.id.comment)
        action = rootView.findViewById(R.id.action)
        action.setOnClickListener(onActionClickListener)
        rootView.setOnClickListener(onItemClickListener)
        avatar.setOnClickListener(onAvatarClickListener)
        accost.visibility = View.GONE
        return rootView
    }

    override fun showData(position: Int, itemData: MomentItem) {
        ImageViewUtil.loadImage(avatar, itemData.avatarLoadParam)
        name.text = itemData.user.name
        name.setTextColor(AppContext.getColor(if (itemData.vipIcon.isNotEmpty()) R.color.vip_name else R.color.colorOnSurface))
        ImageViewUtil.loadImage(vipIcon, itemData.vipLoadParam)
        verified.visibility = if (itemData.verified) View.VISIBLE else View.GONE
        TextViewUtil.displayOrGone(content, itemData.feed.text)
        address.text = itemData.feed.address
        images.showData(itemData)
        age.text = itemData.age
        age.setCompoundDrawablesRelativeWithIntrinsicBounds(if (itemData.user.isMale) R.drawable.icon_friend_list_male else R.drawable.icon_friend_list_female, 0, 0, 0)
        age.setBackgroundResource(if (itemData.user.isMale) R.drawable.shape_friend_sex_male else R.drawable.shape_friend_sex_female)
        like.setOnCheckedChangeListener(null)
        like.isChecked = itemData.feed.liked
        like.setOnCheckedChangeListener(onLikeCheckedListener)
        like.text = if (itemData.feed.likeCount == 0) AppContext.getString(R.string.moment_like) else itemData.feed.likeCount.toString()
        comment.text = if (itemData.feed.commentCount == 0) AppContext.getString(R.string.moment_comment) else itemData.feed.commentCount.toString()
        like.setViewTag(itemData)
        action.setViewTag(itemData)
        rootView.setViewTag(itemData)
    }
}